package com.yanghui.controller;

import com.yanghui.pojo.Link;
import com.yanghui.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Controller
public class LinkController {
    @Autowired
    private LinkService linkService;


    /**
     * 添加友情链接
     * 需要考虑是否与某个链接重复
     */
    @RequestMapping("/addLink")
    @ResponseBody
    public Map<String,Object> addLink(Link link) {
        Map<String,Object> map = new HashMap<>();
        /** 返回受影响记录的行数 */
        int count = linkService.addLink(link);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 更新友情链接点击次数
     */
    @RequestMapping("/updateClickNumById")
    @ResponseBody
    public Map<String,Object> updateClickNumById(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        int count = linkService.updateClickNumById(id);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 查询所有已经上架的友情链接
     */
    @RequestMapping("/selectAllLinks")
    @ResponseBody
    public Map<String,Object> selectAllLinks() {
        Map<String,Object> map = new HashMap<>();
        List<Link> linkList = linkService.selectAllLinks();

        if (linkList.size() > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("linkList", linkList);

        return map;
    }
}
